﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Configuration;
using System.Globalization;
using Tihnut.Web.Timer;
using System.Diagnostics;

namespace SurveySystem.Core
{
    abstract class PollingAgent
    {
        private readonly long PollingInterval;

        private string TimerName
        {
            get { return this.GetType().Name + "_timer"; }
        }

        public PollingAgent(string intervalConfigurationItemName)
        {
            PollingInterval =
                int.Parse(ConfigurationManager.AppSettings[intervalConfigurationItemName], CultureInfo.InvariantCulture);
        }

        public void Start()
        {
            Debug.Print("Starting {0} ...",this.GetType().Name);
            Exception dummy;
            IISTimer.RemoveTimer(TimerName);
            IISTimer.SetTimer(TimerName, PollingInterval, ExecutePolling, out dummy);
        }

        private void ExecutePolling(DateTime when, string t_name, IISTimer timer)
        {
            Debug.Print("{0} tick at {1}",TimerName, DateTime.Now);
            Poll();
        }

        protected abstract void Poll();
    }
}
